/**
 * @author emylyano3
 *
 */

package com.gui.windows;

import com.gui.beans.*;
import com.gui.windows.components.*;
import com.design.*;
import com.gui.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

import org.jdesktop.layout.GroupLayout;

/**
 * Singleton class that manage the application main window.
 * */
public class MainWindow extends JFrame implements Sizes{
	
	//**************************************************************************************************
	//	Constructor.
	//**************************************************************************************************
	
	private static MainWindow mainWindow = null;
	
	private MainWindow(String name) {
		super(name);
	}
	
	public static MainWindow getMainWindow (String name) {
		if (mainWindow == null) {
			mainWindow = new MainWindow(name);
		} 
		return mainWindow;
	}
	
	public static MainWindow getMainWindow () {
		if (mainWindow == null) {
			mainWindow = new MainWindow("");
		} 
		return mainWindow;
	}
	
	//**************************************************************************************************
	//	Atributes.
	//**************************************************************************************************
	
	// Components of the main window.
	private ToolPanel toolPanel;
	private SelectionPanel selectionPanel;
	private StatePanel statePanel;
	
	private DesignManager designManager;
	
	// Main Bar of the main window.
	private JMenuBar menuBar;
	
	// draw editor component is the one that acts as a canvas interface. 
	#ifdef HAS_PREVIEW
	public DrawEditor drawEditor;
	#endif
	
	private static final long serialVersionUID = 1L;
	
	//**************************************************************************************************
	//	Methods that exports this Class (Public Methods).
	//**************************************************************************************************
	
	/**
	 * Creates the application main window, that will contains the tools panels, the canvas and
	 * the menu bar.
	 * */
	public void createAndShowGUI(){
		// Components initialization.
	#ifdef HAS_PREVIEW
		MyCanvas canvas =  drawEditor.getCanvas();
	#endif
		designManager = DesignManager.getDesignManager();
		toolPanel = ToolPanel.getToolPanel("Tools");
		selectionPanel = SelectionPanel.getSelectionPanel("Selection Panel");
		statePanel = StatePanel.getStatePanel("State Panel");
		menuBar = MenuBar.getMenuBar(this);
		// layouts settings. 
		GroupLayout layout = new GroupLayout(this.getContentPane());
		this.getContentPane().setLayout(layout);		 
		layout.setAutocreateGaps(true);
		layout.setAutocreateContainerGaps(true);
		// adding panels to main window layout.
		layout.setHorizontalGroup(layout.createParallelGroup()	
				.add(toolPanel)
				.add(selectionPanel)
				.add(statePanel)
			#ifdef HAS_PREVIEW
				.add(canvas.getGeneralPanel())
			#endif
		);
		layout.setVerticalGroup(layout.createSequentialGroup()
				.add(toolPanel)
				.add(selectionPanel)
				.add(statePanel)
			#ifdef HAS_PREVIEW
				.add(canvas.getGeneralPanel())
			#endif
		);
		// main window settings.
		this.setJMenuBar(menuBar);
		this.setIconImage(MySwingTools.loadImage(Resources.ICON_RESOURCES + Resources.MAIN_WINDOW_ICON_NAME + Resources.ICON_EXTENSION));
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(false);
		this.pack();
	}

	//**************************************************************************************************
	//* Getters and setters. 
	//**************************************************************************************************
	
	public ToolPanel getToolPanel() {
		return this.toolPanel;
	}
	
	public SelectionPanel getSelectionPanel() {
		return this.selectionPanel;
	}
	
	public StatePanel getStatePanel() {
		return this.statePanel;
	}
	
	public DesignManager getDesignManager() {
		return this.designManager;
	}
	
	public void updateObjects (Object fromClass) {
		if (fromClass.getClass().toString().equalsIgnoreCase("com.design.DesignManager")) {
			System.out.println("Calling tge selection panel update. ");
			selectionPanel.update();
		}
		
	}
	
	//**************************************************************************************************
	//* Components handler classes. 
	//**************************************************************************************************

#ifdef HAS_PREVIEW	
	private class SetCanvasSizeHandler implements ActionListener {
    	public void actionPerformed (ActionEvent e) {
    		drawEditor.showSetCanvasSizeWindow(MainWindow.this);
    	}
	}
#endif

	/**
	 * Handles the events of the set image button contained in the tool panel.  
	 * */
#ifdef HAS_PREVIEW
	private class SetImagePositionHandler implements ActionListener {
    	public void actionPerformed (ActionEvent e) {
    		Thread thW = new Thread();
    		thW.start();
    		drawEditor.showSetImagePositionWindow();
		 }
	}
#endif

}

